from google.appengine.ext import webapp, gql
from google.appengine.ext.webapp.util import run_wsgi_app
from models import Business, Deal, Comment, CheckIn
from test.test_typechecks import Integer
import util
import string
import logging

##
#this file main role is to handle with rate deals

class DataBaseCheckInSetHandler(webapp.RequestHandler):

    def get(self): 
        self.response.out.write(''' 
        <html> 
          <body> 
            <form method="post"> 
              <p>User_Name: <input type="string" name="user_name" /></p>
              <p>Email: <input type="integer" name="email" /></p>
              <p>Business: <input type="integer" name="business" /></p>
              <p>Facebook_link: <input type="integer" name="facebook_link" /></p>
              <p><input type="submit" /></p>  
            </form> 
          </body> 
        </html> 
        ''')
        
    def post(self):
        logging.info("Start check in set request")
        
        self.response.headers['Content-Type'] = 'text/xml'
        
        #get the xml
        user_name = self.request.get("user_name", "")
        email = self.request.get("email", "")
        pic =  self.request.get("pic", "")
        business_key = self.request.get("business", "")
        facebook_link = self.request.get("facebook_link", "")
        
        logging.debug("business = " + unicode(business_key) + ", email = " + unicode(email) +", pic = " + unicode(pic) + ", facebook_link = " + unicode(facebook_link))
        check_in = CheckIn()
        business = None
        if (business_key != ""):
            business_lst = Business.get([business_key])
            business = business_lst[0]
        
        if (business == None):
            logging.debug("finish check in set request: failed")
            return
        
        logging.debug("found business")
        check_in.business = business
        check_in.email = email
        check_in.pic = pic
        check_in.user_name = user_name
        check_in.facebook_link = facebook_link
        check_in.put()
        logging.debug("finish check in set request: success")
        
class DataBaseCheckInGetHandler(webapp.RequestHandler):

    def get(self): 
        self.response.out.write(''' 
        <html> 
          <body> 
            <form method="post"> 
              <p>Business: <input type="integer" name="business" /></p>
              <p><input type="submit" /></p>  
            </form> 
          </body> 
        </html> 
        ''')
        
    def post(self):
        logging.info("Start check in get request")
        
        self.response.headers['Content-Type'] = 'text/xml'
        
        #get the business_key
        business_key = self.request.get("business", "")
        
        logging.debug("business = " + unicode(business_key))
        
        business = None
        if (business_key != ""):
            business_lst = Business.get([business_key])
            business = business_lst[0]
        
        if (business == None):
            logging.debug("finish check in get request: failed")
            return
        
        
        check_in_list = CheckIn().all()
        check_in_list.filter("business ==", business)
        
        xml_result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
        xml_result += "<root>\n"
        
        for check_in in check_in_list:
            xml_result += unicode(check_in.to_xml())
        
        xml_result += "</root>\n"
        
        logging.debug("xml response:")
        logging.debug(xml_result)
        self.response.out.write(xml_result)
        logging.info("Finish check in request")
        

    
application = webapp.WSGIApplication([('/checkin/set', DataBaseCheckInSetHandler),
                                      ('/checkin/get', DataBaseCheckInGetHandler)], debug=True)


def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()        